﻿using System;
using System.Windows.Forms;
using ACSR.SqlServer.Addin.Core.UI;
using ACSR.SqlServer.AddIn.Core.Interface;
using EnvDTE;
using EnvDTE80;
using ACSR.SqlServer.AddIn.Core.Interfaces;
using ACSR.SqlServer.AddIn.Core.Controllers;

namespace PyAddIn.Adapters
{
    public delegate void AddInMessageEvent(object sender, string message, MessageType messageType);

    public class AddInAdapter : IAddInAdapter
    {
        public CommandEvents CommandEvents
        {
            get
            {
                return _controller.CommandEvents;
            }
        }

        public EnvDTE.AddIn AddInInstance
        {
            get
            {
                return _controller.AddInInstance;
            }
        }
        
        public event AddInMessageEvent OnMessage;
        public event AddInMessageEvent OnWarning;
        public event AddInMessageEvent OnError;
        // public event 
        

        public AddInAdapter(AddInController controller)
        {
            _controller = controller;
        }

       

        
        public void LogWarning(string warning)
        {
            if (OnWarning != null)
            {
                OnWarning(this, warning, MessageType.Message);
            }

        }

        public void LogMessage(string message)
        {
            if (OnMessage != null)
            {
                OnMessage(this, message, MessageType.Message);
            }

        }

        public void LogError(string error)
        {
            if (OnError != null)
            {
                OnError(this, error, MessageType.Message);
            }
        }

        public string CommonUIAssemblyLocation { get; set; }

        public IToolWindowContext CreateCommonUIControl(string typeName, string caption, Guid guid)
        {
            return _controller.CreateCommonUIControl(typeName, caption, guid);
        }

        public void HostWindow(dynamic hostWindow, Control controlToHost)
        {
            _controller.HostWindow(hostWindow, controlToHost);
        }

        public IToolWindowContext CreateHostWindow(Control controlToHost, string caption, string guid)
        {
            return _controller.CreateHostWindow(controlToHost, caption, guid);
        }

        public IToolWindowContext CreateToolWindow(string assemblyLocation, string typeName, string Caption, Guid uiTypeGuid)
        {
            return _controller.CreateToolWindow(assemblyLocation, typeName, Caption, uiTypeGuid);
        }

        public void LockAround(object lockObject, Action<object> callback, object arg)
        {
            lock (lockObject)
            {
                callback(arg);
            }
        }

        public dynamic AddNamedCommand(string name, string buttonText, string toolTip, int ImageId)
        {
            return _controller.AddNamedCommand(name, buttonText, toolTip, ImageId);
        }

        public void ReloadScript()
        {
            this.OnScriptReload(this, new EventArgs());
        }

        #region IAddInAdapter Members


        public event EventHandler OnScriptReload;
        private AddInController _controller;

        #endregion
    }

    
}
